package com.landmesure.settings.fragment.adpter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.landmesure.bluetoothchat.R;
import com.landmesure.settings.fragment.TypeFragment;

import java.util.ArrayList;

public class TypeListAdapter extends BaseAdapter implements TypeFragment.AdapterCallBack {
    private ArrayList<ListInfo> mListInfos;
    private LayoutInflater mInflater;
    private int mPosition;    //记录当前被点击的条目;

    public TypeListAdapter(LayoutInflater inflater, ArrayList<ListInfo> list) {
        mInflater = inflater;
        mListInfos = list;
    }

    @Override
    public int getCount() {
        if (null != mListInfos) {
            return mListInfos.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mListInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.type_list_item_view, null);
            viewHolder.linearLayout = convertView.findViewById(R.id.ll_item_layout);
            viewHolder.imageView = convertView.findViewById(R.id.type_item_iv);
            viewHolder.textView = convertView.findViewById(R.id.type_item_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ListInfo info = (ListInfo) getItem(position);
        viewHolder.linearLayout.setPadding(50 * info.getLevel(), 0, 0, 0 );
        if (info.isExpand()) {
            if (info.getExpanded()) {
                viewHolder.imageView.setImageResource(R.drawable.open_icon);
            } else {
                viewHolder.imageView.setImageResource(R.drawable.close_icon);
            }
        } else {
            viewHolder.imageView.setImageResource(0);
        }
        viewHolder.textView.setText(info.getItemName());
        if (position == mPosition) {
            viewHolder.textView.setTextColor(Color.BLUE);
        } else {
            viewHolder.textView.setTextColor(Color.GRAY);
        }

        return convertView;
    }

    @Override
    public void updateList(int position, ArrayList<ListInfo> infos) {
        mPosition = position;
        mListInfos = infos;
        notifyDataSetChanged();
    }

    private class ViewHolder {
        ImageView imageView;
        TextView textView;
        LinearLayout linearLayout;
    }
}
